package com.uniledger.exchange.bank;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.indexablerv.IndexableEntity;

@Entity
public class BankCard implements Parcelable, IndexableEntity {

    public boolean isDefault;

    @SerializedName("bank_name")
    public String bank;//银行名称
    public String bankCode;//银行编号

    @PrimaryKey
    @NonNull
    public long id;

    @SerializedName("BIN")
    public String bin;

    public String cardName;//卡名字

    public String cardNoLength;//卡号长度
    public String cardType;//银行卡类型 贷记卡 借记卡

    @SerializedName("logo")
    public String bankIcon;//卡标志

    @SerializedName("account_no")
    public String cardNumber;//银行卡号

    public String mobile;//绑定手机号

    @SerializedName("holder_name")
    public String cardHolder;//持卡人

    @SerializedName("country_code")
    public String countryCode;//国家码

    public String countryLocale;//简写
    public String countryEN;//英文
    public String countryZH;//中文

    /**
     * 单币种1 多币种2
     */
    public int cardAssetType;//银行卡对应资产类型
    public String cardAssetSymbol;//货币类型符号

    @SerializedName("Currency")
    public String cardAssetShort;//货币类型简称

    @Ignore
    public String pinyin;

    public String getCardNumber() {
        String originCardNum = cardNumber;
        int len = originCardNum.length();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < len; i++) {
            builder.append(originCardNum.charAt(i));
            if (i == 3 || i == 7 || i == 11 || i == 15 || i == 19) {
                //判断是不是最后一位,最后一位不加空格" "
                if (i != len - 1)
                    builder.append(" ");
            }
        }
        return builder.toString();
    }

    public String getHideCardNumber() {
        return String.format("**** **** **** %1$s",cardNumber.substring(cardNumber.length()-4,cardNumber.length()));
    }

    @Override
    public String toString() {
        return "BankCard{" +
                "bank='" + bank + '\'' +
                ", bankCode='" + bankCode + '\'' +
                ", bin='" + bin + '\'' +
                ", cardName='" + cardName + '\'' +
                ", cardNoLength='" + cardNoLength + '\'' +
                ", cardType='" + cardType + '\'' +
                '}';
    }

    @Ignore
    public BankCard(String cardName, String cardNumber) {
        this.cardName = cardName;
        this.cardNumber = cardNumber;
    }

    public BankCard() {
    }

    @Ignore
    public BankCard(String bank, String cardNumber, String countryZH) {
        this.bank = bank;
        this.cardNumber = cardNumber;
        this.countryZH = countryZH;
    }

    @Override
    public String getFieldIndexBy() {
        return bank;
    }

    @Override
    public void setFieldIndexBy(String indexField) {
        this.bank = indexField;
    }

    @Override
    public void setFieldPinyinIndexBy(String pinyin) {
        this.pinyin = pinyin;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(this.isDefault ? (byte) 1 : (byte) 0);
        dest.writeString(this.bank);
        dest.writeString(this.bankCode);
        dest.writeString(this.bin);
        dest.writeString(this.cardName);
        dest.writeString(this.cardNoLength);
        dest.writeString(this.cardType);
        dest.writeString(this.bankIcon);
        dest.writeString(this.cardNumber);
        dest.writeString(this.mobile);
        dest.writeString(this.cardHolder);
        dest.writeString(this.countryCode);
        dest.writeString(this.countryLocale);
        dest.writeString(this.countryEN);
        dest.writeString(this.countryZH);
        dest.writeInt(this.cardAssetType);
        dest.writeString(this.cardAssetSymbol);
        dest.writeString(this.cardAssetShort);
        dest.writeString(this.pinyin);
    }

    protected BankCard(Parcel in) {
        this.isDefault = in.readByte() != 0;
        this.bank = in.readString();
        this.bankCode = in.readString();
        this.bin = in.readString();
        this.cardName = in.readString();
        this.cardNoLength = in.readString();
        this.cardType = in.readString();
        this.bankIcon = in.readString();
        this.cardNumber = in.readString();
        this.mobile = in.readString();
        this.cardHolder = in.readString();
        this.countryCode = in.readString();
        this.countryLocale = in.readString();
        this.countryEN = in.readString();
        this.countryZH = in.readString();
        this.cardAssetType = in.readInt();
        this.cardAssetSymbol = in.readString();
        this.cardAssetShort = in.readString();
        this.pinyin = in.readString();
    }

    public static final Creator<BankCard> CREATOR = new Creator<BankCard>() {
        @Override
        public BankCard createFromParcel(Parcel source) {
            return new BankCard(source);
        }

        @Override
        public BankCard[] newArray(int size) {
            return new BankCard[size];
        }
    };

    public static List<BankCard> getBankList(){
        List<BankCard> list = new ArrayList<>();

        /**
         * 菲律宾银行卡
         */
        BankCard card1 = new BankCard("Debit Card", "1234 4567 2345 9999");

        card1.bank = "Bangko Sentral Ng Plpinas";
        card1.cardType = "Debit Card";
        card1.mobile = "18610463510";
        card1.cardHolder = "李小义";

        card1.bankIcon = "";

        card1.countryCode = "63";
        card1.countryLocale = "PH";
        card1.countryZH = "菲律宾";
        card1.countryEN = "Philippines";

        card1.cardAssetShort = "PHP";
        card1.cardAssetSymbol = "₱";
        card1.cardAssetType = 1;

        list.add(card1);

        BankCard card2 = new BankCard("Debit Card", "1234 4567 2345 8888");

        card2.bank = "Bangko Sentral Ng Plpinas";
        card2.cardType = "Debit Card";
        card2.mobile = "18610463510";
        card2.cardHolder = "李小义";

        card2.bankIcon = "";

        card2.countryCode = "63";
        card2.countryLocale = "PH";
        card2.countryZH = "菲律宾";
        card2.countryEN = "Philippines";

        card2.cardAssetShort = "PHP";
        card2.cardAssetSymbol = "₱";
        card2.cardAssetType = 1;

        list.add(card2);

        /**
         * 迪拜
         */
        BankCard card3 = new BankCard("Debit Card", "1234 4567 2345 6666");

        card3.bank = "Bangko Sentral Ng Plpinas";
        card3.cardType = "Debit Card";
        card3.mobile = "18610463510";
        card3.cardHolder = "李小义";

        card3.bankIcon = "";

        card3.countryCode = "971";
        card3.countryLocale = "UAE";
        card3.countryZH = "阿联酋";
        card3.countryEN = "United Arab Emirates";

        card3.cardAssetShort = "DH";
        card3.cardAssetSymbol = "¤";
        card3.cardAssetType = 1;

        list.add(card3);

        return list;
    }



}
